package member

import (
	"time"
	"strconv"
	"dhfshop/models/consts"
	"dhfshop/models/member"
	"dhfshop/controllers/base"
	"github.com/astaxie/beego/validation"
)

type ChargeActivityController struct {
	base.BaseController
}

func (this *ChargeActivityController) List() {
	curRoleId := this.Ctx.Input.GetData("curRoleId").(int) 
	companyId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
	//curUserId := this.Ctx.Input.GetData("curUserId").(int)
	query,_,fields,order_by,offset,limit,err := this.GetQueryPara()
	if err != nil {
	   this.ServeError(err.Error())
	   return
	}
	if curRoleId == 1 {
	    if _, ok := query["store_id"]; !ok {      
			this.ServeError("query store_id is not exist")
	        return
		}
	}else if curRoleId == 2 {
	    query["partner_id"] = strconv.FormatInt(companyId, 10)
	}else{
	
	}
	l, cnt, err := member.GetAllBalanceActivitys(query,fields,order_by,offset,limit)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	rsp := struct {
		TotalCount       int64                     `json:"total_count"`
		BalanceActivitys []*member.BalanceActivity `json:"balance_activitys"`
	}{TotalCount: cnt, BalanceActivitys: l}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *ChargeActivityController) Post() {
	var v member.BalanceActivity
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.Name, "name")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return
	}
	v.PartnerId =int64(this.Ctx.Input.GetData("curCompanyId").(int))
	if len(v.StartTimeStr) > 0 {
		v.StartTime, _ = time.Parse(consts.DATETIME_FORMAT,v.StartTimeStr)
	}
	if len(v.EndTimeStr) > 0 {
		v.EndTime, _ = time.Parse(consts.DATETIME_FORMAT,v.EndTimeStr)
	}
	_, err := member.AddBalanceActivity(&v)
	if err != nil {
	   this.ServeError(err.Error())
		return 
	}
	rsp := struct {
		BalanceActivityId int64  `json:"balance_activity_id"`
	    Created       time.Time  `json:"created"`
	}{BalanceActivityId: v.BalanceActivityId, Created:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *ChargeActivityController) Put() {
	v := member.BalanceActivity{}
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.BalanceActivityId, "balance_activity_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	v.PartnerId = int64(this.Ctx.Input.GetData("curCompanyId").(int))
	if len(v.StartTimeStr) > 0 {
	    v.StartTime, _ = time.Parse(consts.DATETIME_FORMAT,v.StartTimeStr)
	}
	if len(v.EndTimeStr) > 0 {
	    v.EndTime, _ = time.Parse(consts.DATETIME_FORMAT,v.EndTimeStr)
	}
	excludeArr := []string{""}
	updateFields, err := this.GetUpdateFields(v, "", "form", excludeArr)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	if err := member.UpdateBalanceActivityById(&v, updateFields...); err != nil{
		this.ServeError(err.Error())
		return
	}
	
	r := struct {
	    BalanceActivityId int64       `json:"balance_activity_id"`
	    Modified          time.Time   `json:"modified"`
	}{BalanceActivityId:v.BalanceActivityId, Modified:time.Now().UTC()}
	this.Data["json"] = r
	this.ServeJSON()
}

func (this *ChargeActivityController) Delete() {
    partnerId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
    //curUserId := this.Ctx.Input.GetData("curUserId").(int)
	v := struct {
		PartnerId          int64    `form:"-"  valid:"Required"`
	    BalanceActivityId  int64  `form:"balance_activity_id" valid:"Required"`
	}{PartnerId: partnerId} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.BalanceActivityId, "balance_activity_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return
	}
	if err := member.DeleteBalanceActivity(v.BalanceActivityId); err != nil {
		this.ServeError(err.Error())
		return
	}
	r := struct {
	    BalanceActivityId int64     `json:"balance_activity_id"`
	    Modified          time.Time `json:"modified"`
	}{BalanceActivityId:v.BalanceActivityId, Modified:time.Now().UTC()}
	this.Data["json"] = r
	this.ServeJSON() 
}

